<html>
<head>
	<script type="text/javascript">
		var aJaxURL			= "server-side/operations/transfer.action.php";		//server side folder url
		var list_aJaxURL	= "server-side/operations/transfer/list.action.php";	//server side folder url
		var seoyURL			= "server-side/seoy/seoy.action.php";					//server side folder url
		var tName			= "example";													//table name
		var fName			= "add-edit-form";												//form name
		
		$(document).ready(function () {
            ClearDB();
            
			LoadTable(tName);
						
			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "");
			
			SetEvents("add_button", "", "", tName, fName, aJaxURL);
		});
		
        //SeoYyy
		$(document.body).click(function (e) {
        	$("#trans_production").autocomplete("close");
        });
        
		function LoadTable(table){
			if(table == "prod_list"){
				var menuLength	= [[10], [10]];
				var local_id	= $("#local_trans_id").val();
				
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
				GetDataTable("prod_list", list_aJaxURL, "get_list", 3, "local_id=" + local_id, 0, menuLength, 1, "asc");
			}else{
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
                GetDataTable(tName, aJaxURL, "get_list", 4, "", 0, "", 0, "desc");
			}
		}
		
		function LoadDialog(form){
			if(form == fName){
				ClearDB();
				
				var id			= $("#trans_id").val();
				
							
				if(empty(id)){
					var local_id	= GetLocalID();
					$("#local_trans_id").val(local_id);
					$("#trans_id").val(local_id);					
				}else{
					$("#local_trans_id").val(id);
				}
				
				/* Dialog Form Selector Name, Buttons Array */
				GetDialog(fName, 600, "auto", "");
				
				GetButtons("add_button_prod", "delete_button_prod");
				LoadTable("prod_list");
				SetEvents("add_button_prod", "delete_button_prod", "check-all-prod", "prod_list", "add-edit-prod-form", list_aJaxURL);	
			}else{
				$("#source_object").attr("disabled", "disabled");  //Disable Object Selector
				var object = $("#source_object").val();
				
				SeoY("trans_production", seoyURL, "transfer_production_name", "obj_id=" + object, 0);
				var buttons = {
			        "save": {
			            text: "დამატება",
			            id: "add-production",
			            click: function () {
			            }
			        }
			    };
				GetDialog("add-edit-prod-form", 500, "auto", buttons);
			}
		}
		
		function CloseDialog(form){
			$("#" + form).dialog("close");
		}
		
	    // Add - Save
	    $(document).on("click", "#save-dialog", function () {
            var trans_id	= $("#local_trans_id").val();
        	var check		= CheckProdList(trans_id);
        	
        	if(check){
		    	param = new Object();
		    	
	            //Action
		    	param.act		= "save_trans";		    
			    
			    param.id		= $("#trans_id").val();    	
			    param.so		= $("#source_object").val();
			    param.to		= $("#target_object").val();
			    
			    if(param.so == param.to){
				    alert("საწყისი და საბოლოო ობიექტი იდენტურია!");
				}else{
				    $.ajax({
				        url: aJaxURL,
					    data: param,
				        success: function(data) {
							if(typeof(data.error) != "undefined")
							{
								if(data.error != "")
								{
									alert(data.error);
								}else
								{
									LoadTable(fName);
					        		CloseDialog(fName);
								}
							}
					    }
				    });
				}
        	}
		});
		
	    // Add - Save Production
	    $(document).on("click", "#add-production", function () {
	    	param = new Object();
	    	
            //Action
	    	param.act		= "save_prod";		    
		    
		    param.id		= $("#production_id").val();	    	
		    param.l_id		= $("#local_trans_id").val();
		    param.tn		= $("#trans_production").val();
		    param.tq		= $("#trans_quantity").val();
		    param.so		= $("#source_object").val();		    
	    	
	    	if(param.tn == ""){
				alert("შეავსეთ პროდუქტის სახელი!");
			}else if(param.tq == ""){
				alert("შეავსეთ პროდუქტის რაოდენობა!");
			}else {
			    $.ajax({
			        url: list_aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined")
						{
							if(data.error != "")
							{
								alert(data.error);
							}else
							{
								LoadTable("prod_list");
				        		CloseDialog("add-edit-prod-form");
							}
						}
				    }
			    });
			}
		});
		
        $(document).on("click", ".combobox", function (event) {
	    	var i = $(this).text();
			$("#" + i).autocomplete( "search", "" );
		});
		
        function CheckProdList(trans_id) {
            var check = false;
            $.ajax({
                url: aJaxURL,
                async: false, //r-value
                data: "act=check_prod_list&id=" + trans_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            check = true;
                        }
                    }
            	}
        	});
            return check;
        }
		
		/**
		* Get next auto incremented id
		* @return {int}   Returns a next id
		*/
		function GetLocalID(){
			var local_id;
			$.ajax({
		        url: aJaxURL,
        		async: false, //r-value
			    data: "act=get_local_id",
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{
							local_id = data.local_id;
						}
					}
			    }
		    });
	      	return local_id;
		}
		
        function ClearDB() {
            $.ajax({
                url: aJaxURL,
                data: "act=clear_db",
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        }
                    }
                }
            });
        }

		$(document).on("keyup", "#trans_quantity", function() {
		    var val = $(this).val();
		    if(isNaN(val)){
			     alert("შეიყვანეთ მხოლოდ ციფრები");
		         val = val.replace(/[^0-9\.]/g,'');
		         if(val.split('.').length>2) 
		             val =val.replace(/\.+$/,"");
		    }
		    $(this).val(val); 
		});
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
            	<h2 align="center">შიდა მოძრაობა</h2>
            	<div id="button_area">
        			<button id="add_button">დამატება</button>
        		</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th style="width: 300px">ოპერაციის თარიღი</th>
                            <th class="aver">საწყისი მდებარეობა</th>
                            <th class="aver">საბოლოო მდებარეობა</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_date" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_start_loc" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_end_loc" value="ფილტრი" class="search_init" />
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="შიდა მოძრაობა">
    	<!-- aJax -->
	</div>
</body>
</html>